package com.study.controller;

import com.study.pojo.dto.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;


@RestController
@RequestMapping("/user")
public class LoginAndRegisterController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @return 返回此用户对象
     */
    @GetMapping("/login")
    public User login(String username, String password) {
        User user = userService.login(username, password);
        System.out.println("login ==> " + user);

        return user;
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public String register(@RequestBody @Validated User user) {
        System.out.println("register => " + user);
        userService.register(user);

        return "注册成功！";
    }

    /**
     * 检测用户名是否可用
     */
    @RequestMapping("/username/exist")
    public String usernameExist(@Null(message = "昵称限制在1-3位中文字符！") @Validated String username) {
        userService.usernameExist(username);

        return "用户名可用";
    }
}
